﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraBars;
using System.IO;
using HRM.DICTIONARY.BLL;
using System.Threading;

namespace HRM.DICTIONARY.DATABASEUI
{
    public partial class xfrmBackupDatabase : DevExpress.XtraEditors.XtraForm
    {
        public xfrmBackupDatabase()
        {
            InitializeComponent();
            this.txtBackupFolder.Text = LoadPath();
            this.txtBackupFile.Text = "HRMExample" + "." + Microsoft.VisualBasic.Strings.Format(DateTime.Now, "dd.MM.yy hh.mm.bak");
        }

        private void TxtBackupFolderPropertiesButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.txtBackupFolder.Text = dialog.SelectedPath;
            }
        }

        public static string LoadPath()
        {
            try
            {
                DataSet set = new DataSet();
                set.ReadXml(Application.StartupPath + @"\LayoutControl\xucBackup.xml");
                return set.Tables[0].Rows[0][0].ToString();
            }
            catch
            {
                List<String> DSDrives = CheckDrive();
                if (DSDrives.Contains("D"))
                    return @"D:\Backup";
                else
                    return @"C:\Backup";
            }
        }
        public static void SavePath(string Path)
        {
            DataSet set = new DataSet();
            using (DataTable table = new DataTable())
            {
                table.Columns.Add("Path", typeof(string));
                object[] objArray = new object[] { Path };
                table.Rows.Add(new object[0]);
                table.Rows[0][0] = objArray[0];
                set.Tables.Add(table);
            }
            try
            {
                set.WriteXml(Application.StartupPath + @"\LayoutControl\xucBackup.xml");
            }
            catch
            {
            }
        }
        protected override bool ProcessDialogKey(System.Windows.Forms.Keys keyData)
        {
            if (keyData == System.Windows.Forms.Keys.Escape)
            {
                base.Close();
                return true;
            }
            return false;
        }

        private void xfrmBackupDatabase_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                if (!Directory.Exists(this.txtBackupFolder.Text))
                {
                    Directory.CreateDirectory(this.txtBackupFolder.Text);
                    if (!Directory.Exists(this.txtBackupFolder.Text))
                    {
                        XtraMessageBox.Show("Không thể khởi tạo được thư mục để lưu trữ dữ liệu!", "Thông Báo");
                        return;
                    }
                }
                SavePath(this.txtBackupFolder.Text);
                this.btnStart.Enabled = false;
                //SYS_LOG.Insert("Cơ Sơ Dữ Liệu", "Sao Lưu", this.txtBackupFolder.Text + ", " + this.txtBackupFile.Text);
                DataController backup = new DataController();
                backup.progess = Bar;
                this.lblMessage.Text = "Đang Sao Lưu...";
                bool KQ = backup.BackupDataBase("toro.hrm", this.txtBackupFolder.Text + @"\" + this.txtBackupFile.Text);
                XtraMessageBox.Show(backup.ErrMsg, "Thông Báo");
            }
            catch(Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Thông Báo");
            }
        }

        private static List<String> CheckDrive()
        {
            List<String> DSDrives = new  List<string>();
            foreach (var drives in DriveInfo.GetDrives())
            {
                DSDrives.Add(drives.Name); 
            }
            return DSDrives;
        }
    }
}